import re
from django.conf import settings
from rest_framework import serializers
from users.models import User
from django_redis import get_redis_connection
from rest_framework_jwt.settings import api_settings
from django.core.mail import send_mail
from celery_tasks.email.tasks import send_email
from itsdangerous import TimedJSONWebSignatureSerializer as TJS



'''
username	str	是	用户名
password	str	是	密码
password2	str	是	确认密码
sms_code	str	是	短信验证码
mobile	str	是	手机号
allow	str	是	是否同意用户协议

'''


class UserSerializer(serializers.ModelSerializer):
    # 额外到字段验证需要显示指明
    password2 = serializers.CharField(max_length=20, min_length=8, write_only=True)
    sms_code = serializers.CharField(max_length=6, min_length=6, write_only=True)
    allow = serializers.CharField(write_only=True)
    token = serializers.CharField(read_only=True)

    class Meta:
        model = User
        fields = ('id','email_active', 'username', 'mobile', 'password', 'password2', 'sms_code', 'allow','token')
        # 对于模型类字段进行额外限制条件添加
        extra_kwargs = {
            'username': {
                'max_length': 20,
                'min_length': 5,
                'error_messages': {
                    'max_length': '名字过长',
                    'min_length': '名字过短'
                }
            },
            'password': {
                'write_only': True,
                'max_length': 20,
                'min_length': 8,
                'error_messages': {
                    'max_length': '密码过长',
                    'min_length': '密码过短'
                }
            }

        }

    # 手机号验证
    def validate_mobile(self, value):

        print(value)

        if not re.match(r'^1[3-9]\d{9}$', value):
            return serializers.ValidationError('手机号不正确')

        return value

    # 选中状态
    def validate_allow(self, value):

        if value != 'true':
            return serializers.ValidationError('状态未选中')

        return value

    def validate(self, attrs):
        # 密码判断
        if attrs['password'] != attrs['password2']:
            return serializers.ValidationError('密码不一致')

        # 短信验证
        # 从缓存中取出短息
        conn = get_redis_connection('verify')
        sms_code_real = conn.get('sms_code_%s' % attrs['mobile'])
        # 获取不到短信
        if not sms_code_real:
            return serializers.ValidationError('短信失效')

        if attrs['sms_code'] != sms_code_real.decode():
            return serializers.ValidationError('短信不一致')

        return attrs


    def create(self, validated_data):

        print(validated_data)
        # 删除不需要保存的字段数据
        del validated_data['password2']
        del validated_data['sms_code']
        del validated_data['allow']

        user=super().create(validated_data)
        # 对于明文密码加密
        user.set_password(validated_data['password'])
        user.save()


        # 生成ｊｗｔ -----token值返回

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

        # 对用户对象添加ｔｏｋｅｎ属性
        user.token = token



        return user


class UserDetailSerializer(serializers.ModelSerializer):


    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'email', 'email_active')


class UserEmailSerializer(serializers.ModelSerializer):


    class Meta:
        model = User
        fields = ('id', 'email')
        extra_kwargs = {
            "email":{"required":True}
        }

    def update(self, instance, validated_data):
        # 获取验证后的ｅｍａｉｌ
        email = validated_data["email"]
        # 通过context获取
        user = self.context["request"].user
        # 更新当前用户的email数据
        user.email = email
        user.save()


        data = {"id":user.id,"username":user.username}
        # 加密数据
        tjs = TJS(settings.SECRET_KEY,300)
        token = tjs.dumps(data).decode()
        verify_url = 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token

        # 发送邮箱
        # msg = '<a href="http://www.meiduo.site:8080/success_verify_email.html" target="_blank">点击激活</a>'

        # send_mail('注册激活', '', settings.EMAIL_FROM, ['2335476860@qq.com'], html_message=msg)

        send_email.delay(email,verify_url)
        # 结果返回
        return user

